﻿using System;
using System.Data;
using System.Collections.Generic;
using SERP.DAO;
namespace SERP.Controllers{
public class SalaryRulesController : SalaryRulesControllerBase{
	public SalaryRulesController()
		: base()
	{
	}
    public static DataTable GetCalculationTypesTable()
    {
        DataTable tblData = new DataTable();
        tblData.Columns.Add("ID", typeof(int));
        tblData.Columns.Add("Name", typeof(string));
        DataRow r = tblData.NewRow();
        r["ID"] = 0;
        r["Name"] = SERP.Constants.APP_CONSTANT;
        tblData.Rows.Add(r);
        r = tblData.NewRow();
        r["ID"] = 1;
        r["Name"] = SERP.Constants.APP_BYPERSON;
        tblData.Rows.Add(r);
        r = tblData.NewRow();
        r["ID"] = 2;
        r["Name"] = SERP.Constants.APP_BYPERSONMONTH;
        tblData.Rows.Add(r);
        r = tblData.NewRow();
        r["ID"] = 3;
        r["Name"] = SERP.Constants.APP_BYMONTH;
        tblData.Rows.Add(r);
        r = tblData.NewRow();
        r["ID"] = 4;
        r["Name"] = SERP.Constants.APP_BYCALCULATION;
        tblData.Rows.Add(r);
        return tblData;
    }
	private DataTable _Table;
	private SalaryRulesEntity oSalaryRules;	
	public SalaryRulesEntity SalaryRulesObject{
		get{
			return oSalaryRules;
		}
		set{
			oSalaryRules=value;
		}
	}
	public DataTable Table{
		get{
			if(_Table==null&&AllowCaching){				
				_Table=Cache.Get<DataTable>(CACHE_KEY_DS);
			}
			return _Table;
		}
		set{
			_Table=value;
		}
	}
	public void LoadAllDSSalaryRules(bool clearCache){
		bool bForce=clearCache;
		if(bForce||_Table==null)
		{
			DataSet ds=Service.SelectAllDS();
			_Table=ds.Tables[0];
			AddIndexToTable(_Table);
			if(AllowCaching)
				AddCache(CACHE_KEY_DS,_Table);			
		}				
	}	
	public void AddIndexToTable(){
		AddIndexToTable(_Table);
	}
	public void ResetTableIndex(){
		ResetTableIndex(_Table);
	}
	public void LoadObject(Int32 SalaryRuleId){
		SalaryRulesEntity oLoad=FindSalaryRules(SalaryRuleId);
		if(oLoad==null){
			LoadAllSalaryRules(true);
			oSalaryRules=FindSalaryRules(SalaryRuleId);
		}
		else{
			oSalaryRules=oLoad;
		}
	}
	public bool AddNew(){
		if(oSalaryRules==null)return false;
		bool result = Service.Save(oSalaryRules);
		if(result){
			AddObjectToCache(oSalaryRules);
		}
		return result;
	}
	public bool Update(){
		if(oSalaryRules==null)return false;
		return Service.Update(oSalaryRules);	
	}
	public bool Delete(){
		if(oSalaryRules==null)return false;		
		bool retvalue = Service.Delete(oSalaryRules);
		if(retvalue){			
			RemoveObjectFromCache(oSalaryRules);
		}
		return retvalue;
	}	
	public bool DeleteByID(Int32 SalaryRuleId){
		bool retvalue = Service.DeleteByID(SalaryRuleId);		
		if(retvalue){
			SalaryRulesEntity oDeleted=FindSalaryRules(SalaryRuleId);
			RemoveObjectFromCache(oDeleted);
		}
		return retvalue;
	}	
	public void ResetObject(){
		this.oSalaryRules = new SalaryRulesEntity();
	}
	public void MapObjectToDataRow(DataRow row){
		this.MapObjectToDataRow(this.SalaryRulesObject,row);
    }
    public SalaryRulesEntity LoadSalaryRules(Int32 SalaryRuleId){			
		SalaryRulesEntity o=Service.SelectOne(SalaryRuleId);		
		return o;
	}
	#region Cached
    private static List<SalaryRulesEntity>_ListSalaryRules;
	private static bool _AllowCaching=true;
	public static bool AllowCaching{
		get{return _AllowCaching;}
		set{AllowCaching=value;}
	}	
	public static void ClearCache(){
			if(!_AllowCaching)return;
			Cache.Remove(CACHE_KEY_DS);
			Cache.Remove(CACHE_KEY_LS);
	}
	private const string CACHE_KEY_DS="SalaryRulesDS";
	private const string CACHE_KEY_LS="SalaryRulesLS";
	private void AddObjectToCache(SalaryRulesEntity newObject){
		if(!AllowCaching)return;
		GetCacheList().Add(newObject);
	}
	private void RemoveObjectFromCache(SalaryRulesEntity oRemove)
	{
		if(!AllowCaching)return;
		if(oRemove!=null)
		GetCacheList().Remove(oRemove);
	}
	private static List<SalaryRulesEntity>GetCacheList(){
		if(Cache==null)return default(List<SalaryRulesEntity>);
		List<SalaryRulesEntity>ls= Cache.Get<List<SalaryRulesEntity>>(CACHE_KEY_LS);
		if(ls==null){
			ls = new List<SalaryRulesEntity>();
			AddCache(CACHE_KEY_LS,ls);
		}
		return ls;
	}	
	public static Dictionary<Int32,SalaryRulesEntity> GetSalaryRulesDictionary(){
		Dictionary<Int32,SalaryRulesEntity> dic=new Dictionary<Int32,SalaryRulesEntity>();
		foreach(SalaryRulesEntity o in ListSalaryRulesStatic){
			dic.Add(o.SalaryRuleId,o);
		}
		return dic;
	}
	public List<SalaryRulesEntity>ListSalaryRules{
		get{
			return LoadAllSalaryRules(false);
		}
	}
	public static List<SalaryRulesEntity>ListSalaryRulesStatic{
		get{
			LoadAllSalaryRules(false);
			return _ListSalaryRules;
		}
	}
	public SalaryRulesEntity FindSalaryRules(Int32 SalaryRuleId){
		if(AllowCaching){
			_ListSalaryRules=GetCacheList();
		}		
		SalaryRulesEntity oResult=_ListSalaryRules.Find(delegate(SalaryRulesEntity oFind){
			bool result=true; 
			result&=oFind.SalaryRuleId.Equals(SalaryRuleId);
			return result;
		 });
		return oResult;
	}
	public static List<SalaryRulesEntity>LoadAllSalaryRules(bool clearCache){
		_ListSalaryRules = new List<SalaryRulesEntity>();
		if(AllowCaching){
			_ListSalaryRules=GetCacheList();
		}
		if(_ListSalaryRules==null||_ListSalaryRules.Count==0)
			clearCache=true;
		if(!clearCache)return _ListSalaryRules;
		_ListSalaryRules=new List<SalaryRulesEntity>(Service.SelectAll());		
		if(AllowCaching)
			AddCache(CACHE_KEY_LS,_ListSalaryRules);
		return _ListSalaryRules;
	}
	#endregion
}
}
